package com.foreknow.elm.dao.Impl;

import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderdetailetDaoImpl implements OrderdetailetDao {
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public List<Orderdetailet> getOD(Integer orderId) throws SQLException {
        List<Orderdetailet> list=new ArrayList<>();
        String sql="SELECT od.*,f.* FROM orders o JOIN business b on o.businessId=b.businessId JOIN orderdetailet od on o.orderId=od.orderId JOIN food f on od.foodId=f.foodId and o.orderId=?";
        try {
            conn=DBUtil.getConnection();
            pst= conn.prepareStatement(sql);
            pst.setInt(1,orderId);
            rs= pst.executeQuery();
            while (rs.next()){
                Orderdetailet orderdetailet=new Orderdetailet();
                orderdetailet.setOdId(rs.getInt("OdId"));
                orderdetailet.setOrderId(rs.getInt("OrderId"));
                orderdetailet.setFoodId(rs.getInt("FoodId"));
                orderdetailet.setQuantity(rs.getInt("Quantity"));
                Food food=new Food();
                food.setFoodId(rs.getInt("FoodId"));
                food.setFoodName(rs.getString("FoodName"));
                food.setFoodExplain(rs.getString("FoodExplain"));
                food.setFoodImg(rs.getString("FoodImg"));
                food.setFoodPrice(rs.getDouble("FoodPrice"));
                food.setBusinessId(rs.getInt("BusinessId"));
                food.setRemarks(rs.getString("Remarks"));
                orderdetailet.setFood(food);
                list.add(orderdetailet);
            }
        }finally {
            DBUtil.close(pst);
        }
        return list;
    }

//    @Override
//    public List<Orderdetailet> getOD(String userId) throws SQLException {
//        List<Orderdetailet> list=new ArrayList<>();
//        String sql="SELECT od.* FROM orders o JOIN business b on o.businessId=b.businessId JOIN orderdetailet od on o.orderId=od.orderId and o.userId=?";
//        try {
//            conn=DBUtil.getConnection();
//            pst= conn.prepareStatement(sql);
//            pst.setString(1,userId);
//            rs= pst.executeQuery();
//            while (rs.next()){
//                Orderdetailet orderdetailet=new Orderdetailet();
//                orderdetailet.setOdId(rs.getInt("OdId"));
//                orderdetailet.setOrderId(rs.getInt("OrderId"));
//                orderdetailet.setFoodId(rs.getInt("FoodId"));
//                orderdetailet.setQuantity(rs.getInt("Quantity"));
//                list.add(orderdetailet);
//            }
//        }finally {
//            DBUtil.close(pst);
//        }
//        return list;
//    }

    @Override
    public int OrdersDeInsert(List<Orderdetailet> list) throws SQLException {
        int a=0;
        String sqlInsertOrderDetailet="INSERT INTO orderdetailet (orderId,foodId,quantity) VALUES ";
        StringBuffer sql=new StringBuffer(sqlInsertOrderDetailet);
        try {
            conn= DBUtil.getConnection();
            for (int i=0;i< list.size();i++) {
                sql.append("(?,?,?) ");
                pst = conn.prepareStatement(String.valueOf(sql));
                pst.setInt(1, list.get(i).getOrderId());
                pst.setInt(2, list.get(i).getFoodId());
                pst.setInt(3, list.get(i).getQuantity());
                a+=pst.executeUpdate();
                sql.delete(sql.lastIndexOf("("), sql.lastIndexOf(" "));
            }
        }finally {
            DBUtil.close(pst);
        }
        return a;
    }

    public static void main(String[] args) throws SQLException {
        OrderdetailetDao odd =new OrderdetailetDaoImpl();
//        List<Orderdetailet> list=odd.getOD(6);
//        for (int i=0;i< list.size();i++) {
//            System.out.println(list.get(i).getOdId()+"---"+ list.get(i).getFoodId()+"---"+list.get(i).getFood().getFoodName());
//        }
    }
}
